//
//  NewFeatureCollectionViewController.swift
//  我的微博练习
//
//  Created by zhangguo on 15/10/12.
//  Copyright © 2015年 zhangguo. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
class NewFeatureCollectionViewController: UICollectionViewController {
    //图像总数
    let imageCount = 4
    private let layout = GFFlowLayout()
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       
        return imageCount
    }
    
     init() {
        super.init(collectionViewLayout: layout)
    }
    required init?(coder aDecoder: NSCoder) {
        
        //本类禁止用sb开发 纯代码，风险小 考虑因数少
        //fatalError("init(coder:) has not been implemented")
        //本类允许用sb开发 有些公司是混合开发，都需要测试
        super.init(coder: aDecoder)
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as? NewFeatureCell
        
        cell!.imageIndex = indexPath.item
        
        return cell!
    }
    
    //完成显示cell
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        let path = collectionView.indexPathsForVisibleItems().last!
        
        //判断是否是末尾的图片
        if path.item == imageCount - 1 {
            //播放动画
            let cell = collectionView.cellForItemAtIndexPath(path) as? NewFeatureCell
            cell!.startAnimation()
        }
    }

    //性特性cell
class NewFeatureCell:UICollectionViewCell {
    
    //图像索引
    private var imageIndex: Int = 0{
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            
            startAnimation()
            startButton.hidden = true
        }
    }
    
    //开始体验
    func clickStartButton() {
        NSNotificationCenter.defaultCenter().postNotificationName(GFRootViewCotrollerSwitchNotification, object: true)
    }
    
    //开始按钮动画
    private func startAnimation() {
        startButton.hidden = false
            
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            //h回复默认形变
            self.startButton.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
        self.startButton.userInteractionEnabled = true
        }
        
    }
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            prepaUI()
        }

        required init?(coder aDecoder: NSCoder) {
           // fatalError("init(coder:) has not been implemented")
            super.init(coder: aDecoder)
            prepaUI()
        }
        
        private func prepaUI() {
            
           //添加控件
            contentView.addSubview(iconView)
            contentView.addSubview(startButton)
            //自动布局
            iconView.ff_Fill(contentView)
   
            //按钮布局
            startButton.ff_AlignHorizontal(type: ff_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPointMake(0, -160))
        }
    
    //懒加载控件
    private lazy var iconView = UIImageView()
    private lazy var startButton: UIButton = {
    let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
          button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        ///大小
        button.sizeToFit()
        
        //监听按钮方法
        button.addTarget(self, action: "clickStartButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
}
    //自定义流水布局
    private class GFFlowLayout:UICollectionViewFlowLayout {
     
        private override func prepareLayout() {
        
            itemSize = collectionView!.bounds.size
            minimumInteritemSpacing = 0
            minimumLineSpacing = 0
            scrollDirection = UICollectionViewScrollDirection.Horizontal
            
            collectionView?.pagingEnabled = true
            collectionView?.showsHorizontalScrollIndicator = false
            collectionView?.bounces = false
        
        }
    }
}
